package com.example.zhihuijiaju;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


// 状态变化监听
interface HttpChangeListener {
    // 回调方法
    void HttpChange(Response response);
}

enum Method {
    POST, GET;
}

public class HttpLink {
    private static final String TAG = "HttpLink";
    private String mtype = "application/json; charset=utf-8";
    private String ToKen = "";
    private String url;
    private String JsonData = "{}";
    private Method method;

    HttpChangeListener httpChangeListener;

    public HttpLink(String ToKen,String url,String JsonData,Method method)
    {
        this.ToKen = ToKen;
        this.url = url;
        this.JsonData = JsonData;
        this.method = method;
    }

    // 提供注册事件监听的方法

    void HttpListener(HttpChangeListener httpChangeListener) throws JSONException
    {
        this.httpChangeListener = httpChangeListener;
        //因为是模拟，所以需要在此将控件状态传出

        //Log.i(TAG,"jsonObject直接创建json:" + jsonObject);

        //1 . 拿到OkHttpClient对象
        OkHttpClient client = new OkHttpClient();
        //创建一个RequestBody(参数1：数据类型 参数2传递的json串)
        Log.i(TAG,JsonData);
        JSONObject jsonObject =  JSON.parseObject(JsonData);
        MediaType JSON = MediaType.parse(mtype);
        RequestBody requestBody = null;
        requestBody = RequestBody.create(JSON, jsonObject.toJSONString());
            //3 . 构建Request,将FormBody作为Post方法的参数传入
        Request request = null;

        Log.i(TAG,url);

        //判断请求方法
        if(method==Method.POST&&ToKen==""){
            request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
        }
        else if(method==Method.GET&&ToKen==""){
            request = new Request.Builder()
                    .url(url)
                    .build();
        }
        if(method==Method.POST&&ToKen!=""){
            request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .header("X-Auth-Token",ToKen)
                    .build();
        }
        else if(method==Method.GET&&ToKen!=""){
            request = new Request.Builder()
                    .url(url)
                    .header("X-Auth-Token",ToKen)
                    .build();
        }


        Request finalRequest = request;
        new Thread(){
            @Override
            public void run() {
                super.run();

                //准备好call对象
                Call call = client.newCall(finalRequest);
                //同步请求
                Log.i(TAG,"等待请求结束");
                try {
                    Response response = call.execute();
                  //  Log.i(TAG,response.body().string());
                    IscClick(response);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    //回调
    public void IscClick(Response response) {
        // 通过接口对象传控件状态
        httpChangeListener.HttpChange(response);
    }

}
